package com.thlws.study.rabbitmq.internal.receiver;

import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author HanleyTang 2019-06-23
 */
@Component
public class TopicReceiver {

    private final Logger logger = LoggerFactory.getLogger(getClass());


    /***
     * 自动创建队列
     * Exchange（TOPIC） 与 Queue绑定
     * 指定 routing key
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topicQueue1"),
            key = "user.#",
            exchange = @Exchange(value = "myTopicExChange",type = ExchangeTypes.TOPIC)
    ))
    public void topicQueue1(Message message, Channel channel) throws IOException {
        logger.info("topicQueue1-user.#: {}", message);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topicQueue2"),
            key = "user.info",
            exchange = @Exchange(value = "myTopicExChange",type = ExchangeTypes.TOPIC)
    ))
    public void topicQueue2(Message message, Channel channel) throws IOException {
        logger.info("topicQueue2-user.info: {}", message);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
